package cn.lanqiao.ui;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.lanqiao.entity.Account;
import cn.lanqiao.entity.User;
import cn.lanqiao.service.UserService;
import cn.lanqiao.serviceimpl.UserServiceImpl;

public class LoginFrm extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private ImageIcon icon = new ImageIcon("src/kinghonor.jpg");
	private ImageIcon image = new ImageIcon("src/kinghonor1.gif");
	JLabel lblNewLabel_1;
	private int width = 700;
	private int height = 394;
	private AudioClip clip;
	private File file;
	private URL url;
	private java.net.URI uri;
	private boolean press = true;//控制背景音乐的开关
	JPasswordField password;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrm frame = new LoginFrm();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrm() {
		setTitle("王者荣耀助手登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 699, 711);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		contentPane.setLayout(null);
		
		lblNewLabel_1 = new JLabel("New label");
		lblNewLabel_1.setBounds(0, 0, 694, 394);
		lblNewLabel_1.setIcon(new ImageIcon("src\\cn\\lanqiao\\source\\kinghonor1.gif"));
		contentPane.add(lblNewLabel_1);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 396, 694, 283);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JButton btnNewButton_1 = new JButton("我的账号");
		btnNewButton_1.setBounds(14, 111, 113, 27);
		panel.add(btnNewButton_1);
		
		JButton btnNewButton_2 = new JButton("历史账号");
		btnNewButton_2.setBounds(14, 151, 113, 27);
		panel.add(btnNewButton_2);
		
		JLabel lblNewLabel = new JLabel("版本 1.0.0(正式版本 x64)");
		lblNewLabel.setBounds(14, 229, 191, 18);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_2 = new JLabel("June 5 2020");
		lblNewLabel_2.setBounds(14, 260, 96, 18);
		panel.add(lblNewLabel_2);
		
		JButton button_1 = new JButton("退出");//退出程序
		button_1.setBounds(567, 205, 113, 27);
		button_1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int option = JOptionPane.showConfirmDialog(null, "确认退出?", "退出提醒", JOptionPane.YES_NO_OPTION);
				if (option == 0) {
					System.exit(0);
				}
				return;
			}
		});
		panel.add(button_1);
		
		JButton button_2 = new JButton("服务条款");
		button_2.setBounds(567, 119, 113, 27);
		panel.add(button_2);
		
		JButton button_3 = new JButton("制作人员");
		button_3.setBounds(567, 79, 113, 27);
		button_3.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				BeginMakerJDialog beginJDialog = new BeginMakerJDialog();
				beginJDialog.setVisible(true);
			}
		});
		panel.add(button_3);
		
		JButton button_4 = new JButton("片头动画");
		button_4.setBounds(567, 39, 113, 27);
		button_4.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				
				LoginFrm.this.lblNewLabel_1.setIcon(LoginFrm.this.icon);
			}
		});
		panel.add(button_4);
		
		JLabel label = new JLabel("用户名");
		label.setBounds(302, 43, 72, 18);
		panel.add(label);
		
		textField = new JTextField();
		textField.setBounds(239, 72, 191, 24);
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel label_1 = new JLabel("密码");
		label_1.setBounds(302, 115, 72, 18);
		panel.add(label_1);
		
		password = new JPasswordField();
		password.setBounds(239, 152, 191, 24);
		panel.add(password);
		password.setColumns(10);
		
		JButton button = new JButton("登录");
		button.setBounds(271, 189, 113, 27);
		button.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				UserService userService = new UserServiceImpl();
				User user = userService.getUser(LoginFrm.this.textField.getText());
				Account account = userService.enterSystem(LoginFrm.this.textField.getText());
				String passwordOne = account.getPassword();
				String passwordTwo = new String(LoginFrm.this.password.getPassword());
				if (passwordOne.equalsIgnoreCase(passwordTwo)) {
					UserFrm userFrm = new UserFrm(user);
					userFrm.setVisible(true);
					LoginFrm.this.setVisible(false);
				}else {
					JOptionPane.showConfirmDialog(null, "用户名或密码有误", "登录出错", JOptionPane.YES_NO_OPTION);
					return;
				}
			}
		});
		panel.add(button);
		
		JButton btnNewButton = new JButton("停止播放");
		btnNewButton.setBounds(14, 13, 113, 27);
		btnNewButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (btnNewButton.getText().equals("播放音乐")) {
					btnNewButton.setText("停止播放");
				}else {
					btnNewButton.setText("播放音乐");
				}
			}
		});
		panel.add(btnNewButton);
		this.setResizable(false);
		this.setVisible(true);
		init();
		playMusic();
		if (btnNewButton.getText().equals("播放音乐")) {
			stopMusic();
		}else {
			playMusic();
		}
	}
	//初始化音频
	private void init() {
		String audioPath;
		try {
			audioPath = "src\\cn\\lanqiao\\source\\英雄归来.au";
			this.file = new File(audioPath);
			this.uri = this.file.toURI();
			this.url = uri.toURL();
			this.clip = Applet.newAudioClip(url);//创建播放音频对象
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("音频播放失败");
		}		
	}
	//停止播放
	private void stopMusic() {
		this.clip.stop();
	}
	//播放
	private void playMusic() {
		this.clip.play();
	}
	//循环播放
	private void loopPlayMusic() {
		this.clip.loop();
	}
}
